<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Circle extends Modules_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    public function index($cid=false,$page=1)
    {
        // 栏目路径
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];

        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        $cid=$this->cid;
        $where1 = array("a.cid"=>$cid,"a.ccid"=>$this->ccid);
        $arr = array();
        if(isset($_GET['uname']) and $_GET['uname']){
            $arr['like']=array('b.uname',$_GET['uname']);
        }
        $where = array_merge($arr,$where1);
        $this->db->select('a.id,a.audit,a.flag,a.content,a.r_content,a.comments,a.rid,a.timeline,b.uname');
        $this->db->from('boc_circle as a');
        $this->db->join('boc_account as b','a.uid=b.id','left');
        $this->db->where($where);
        $this->db->order_by('a.timeline','desc');
        $this->db->limit($limit,$limit*($page-1));
        $vdata['list']=$this->db->get()->result_array();
        //分页
        $this->db->select('a.id,a.audit,a.content,a.rid,a.timeline,b.uname');
        $this->db->from('boc_circle as a');
        $this->db->join('boc_account as b','a.uid=b.id','left');
        $this->db->where($where);
        $all = $this->db->get()->result_array();
        $count = count($all);
        $vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$count,4);
        $this->_display($vdata);
    }

    protected function _pages($url,$per_page=-1,$count=0,$uri=3){
        if (!$per_page < 0) {
            $per_page = $this->page_limit;
        }
        $this->load->library('pagination');
        $this->pagination->initialize(page_config($per_page,$count,$url,$uri));
        return $this->pagination->create_links();
    }

    // 审核 POST (对于开关类型字段，拷贝审核修改相应的audit字段即可使用)
    public function audit($key = FALSE){
        if (!$key AND $this->input->post('ids') ) {
            $key = explode(',',$this->input->post('ids'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_id'));
        }
        $where = FALSE;

        if ($this->input->get('c')) {
            $where = array('cid' => $this->input->get('c'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_col_id'));
        }

        $audit = $this->input->post('audit');

        if ($audit) {
            $audit=1;
        }else{
            $audit=0;
        }
        $msg = array('禁用成功','成功启用');

        if (!isset($vdata['status'])) {
            if ($where) {
                $res = $this->model->audit($audit,$key,$where);
            }else{
                $res = $this->model->audit($audit,$key);
            }
            if ($res) {
                $vdata = array('status'=>1,'msg'=>$msg[$audit]);
                if (is_array($key)) {
                    $this->mlogs->add('audit',lang('modules_audit_id').$this->input->post('ids').lang('modules_audit_for').$audit);
                }else{
                    $this->mlogs->add('audit',lang('modules_audit_id').$key.lang('modules_audit_for').$audit);
                }
            }else{
                $vdata = array('status'=>0,'msg'=>lang('modules_audit_err_select'));
            }
        }

        if($this->input->is_ajax_request()){
            $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
        }else{
            $this->load->view('msg',$vdata);
        }
    }
}
